<template>
  <list ref="list"
        :showRefresh="true"
        :showLoadMore="showLoadMore"
        @refresh="refresh('list')"
        @loadMore="loadmore('list')"
        loadmoreoffset="100">

    <cell v-for="(log, index) in data"
          :key="index">
      <div class="td-row">
        <text class="td-serial">{{log.house_no}}</text>
        <text class="td-text">{{log.log_time}}</text>
        <text class="td-text">{{log.die_num}}</text>
        <text class="td-text">{{log.eli_num || '-'}}</text>
        <text class="td-text">{{log.feed_product_name || '-'}}</text>
      </div>
    </cell>

  </list>
</template>
<script>
import {
  STORAGE_FARM
} from '../../plugins/constant'

export default {
  components: {},
  props: {},
  data() {
    return {
      farm: {},
      params: {
        index: 1,
        size: 20,
        'supplier_id.eq': '',
        'purchase_no.eq': ''
      },
      totalPages: 2,
      data: []
    }
  },
  computed: {
    showLoadMore() {
      return this.totalPages > this.params.index
    }
  },
  watch: {},
  created() {
    this.getFarm()
  },
  mounted() { },
  methods: {
    getFarm() {
      this.farm = this.$storage.getSync(STORAGE_FARM) || {}
      if (this.farm.id) {
        this.refresh()
      }
    },
    refresh() {
      this.params.index = 1
      this.getData().then(res => {
        this.data = res
      }).finally(() => {
        this.$refs['list'].refreshEnd()
      })
    },
    loadmore() {
      this.getData().then(res => {
        this.data.push(...res)
      }).finally(() => {
        this.$refs['list'].loadMoreEnd()
      })
    },
    getData() {
      return new Promise((resolve, reject) => {
        this.$api.getData({
          server: 'agr_chain',
          table: 'base_produce_log_view',
          params: this.params
        }).then(res => {
          this.params.index++
          this.totalPages = (res && res.totalPages) || 0
          const data = (res && res.data) || []
          resolve(data)
        }).catch(err => {
          this.totalPages = -1
          reject(err)
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "../../css/variables.scss";
</style>
